/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.regulacion.service;

import java.util.Date;
import java.util.List;

import co.gov.medellin.sdep.sisdep.common.dto.GenericComponentVO;
import co.gov.medellin.sdep.sisdep.common.message.MessageDescriptorConstant;
import co.gov.medellin.sdep.sisdep.common.service.BaseService;
import co.gov.medellin.sdep.sisdep.model.Modulo;
import co.gov.medellin.sdep.sisdep.model.dao.impl.IModuloDAOT;
import co.gov.medellin.sdep.sisdep.regulacion.exception.ModuloException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.ModuloRuntimeException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.descriptor.ModuloMessageDescriptor;

/**
 * <p>
 * Implementacion de todos los servicios servicios necesarios para la gestion de los Modulos asignados a comercientes en SISDEP.
 * </p>
 * <p>
 * Copyright (c) 2012<BR>
 * Consult-Soft S.A.<BR>
 * Created on: May 01, 2012<BR>
 * 
 * @author atabares
 * </p>
 */
public class ModuloServiceImpl extends BaseService implements IModuloService {

	/** Serial key. */
	private static final long serialVersionUID = -8925811278546659555L;
	
	/** Interface de acceso a los DAOs de Modulo. */
	private IModuloDAOT moduloDAO;

	
	/** Constructor por defecto. */
	public ModuloServiceImpl() {
		
	}
	
	/**
	* Obtiene el parametro moduloDAO.
	* @see ModuloServiceImpl#moduloDAO
	*
	* @return El parametro moduloDAO.
	*/
	public IModuloDAOT getModuloDAO() {
		return moduloDAO;
	}

	/**
	* Asigna el parametro moduloDAO.
	* @see ModuloServiceImpl#moduloDAO
	*
	* @param moduloDAO valor a asignar al parametro moduloDAO.
	*/
	public void setModuloDAO(IModuloDAOT moduloDAO) {
		this.moduloDAO = moduloDAO;
	}

	
	/* (non-Javadoc)
	 * @see co.gov.medellin.sdep.sisdep.regulacion.service.IModuloService#insert(co.gov.medellin.sdep.sisdep.model.Modulo)
	 */
	public void insert(Modulo modulo) throws ModuloException {
		try{
			modulo.setUsuarioCrea(getPrincipalUser());
			modulo.setFechaCrea(new Date());
			getModuloDAO().insert(modulo);
		}catch (Exception e) {
			Object [] infoError = {
				modulo.getCodigo()
			};
			
			if (e.getCause().toString().toUpperCase().indexOf("ORA-00001") >= 0) {
				throw new ModuloException(new ModuloMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			} else {
				throw new ModuloException(new ModuloMessageDescriptor(MessageDescriptorConstant.SAVE), infoError, e);
			}
		}
	}

	/* (non-Javadoc)
	 * @see co.gov.medellin.sdep.sisdep.regulacion.service.IModuloService#update(co.gov.medellin.sdep.sisdep.model.Modulo)
	 */
	public final void update(Modulo modulo) throws ModuloException {
		try{
			modulo.setUsuarioModifica(getPrincipalUser());
			modulo.setFechaModifica(new Date());
			getModuloDAO().update(modulo);
		}catch (Exception e) {
			Object [] infoError = {
				modulo.getCodigo()
			};
			
			if (e.getCause().toString().toUpperCase().indexOf("ORA-00001") >= 0) {
				throw new ModuloRuntimeException(new ModuloMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			} else {
				throw new ModuloRuntimeException(new ModuloMessageDescriptor(MessageDescriptorConstant.UPDATE), infoError, e);
			}
		}
	}

	/* (non-Javadoc)
	 * @see co.gov.medellin.sdep.sisdep.regulacion.service.IModuloService#delete(co.gov.medellin.sdep.sisdep.model.Modulo)
	 */
	public final void delete(Modulo modulo) throws ModuloException {
		try {
			modulo.setUsuarioModifica(getPrincipalUser());
			modulo.setFechaModifica(new Date());
			getModuloDAO().delete(modulo);
		}catch (Exception e) {
			Object [] infoError = {
				modulo.getCodigo()
			};
			throw new ModuloRuntimeException(new ModuloMessageDescriptor(MessageDescriptorConstant.DELETE), infoError, e);
		}
	}

	/* (non-Javadoc)
	 * @see co.gov.medellin.sdep.sisdep.regulacion.service.IModuloService#load(int, int, co.gov.medellin.sdep.sisdep.model.Modulo)
	 */
	public final List load(int start,int max, Modulo modulo) throws ModuloException {
		try{
			//return getModuloDAO().getAllModuloPaginated(start, max, criteria);
			return getModuloDAO().load();
		}catch (Exception e) {
			Object [] infoError = null;
			throw new ModuloRuntimeException(new ModuloMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	/* (non-Javadoc)
	 * @see co.gov.medellin.sdep.sisdep.regulacion.service.IModuloService#get(long)
	 */
	public final Modulo get(long id) throws ModuloException {
		try{
			return getModuloDAO().findByPrimaryKey(id);
		}catch (Exception e) {
			Object [] infoError = {new Long(id)};
			throw new ModuloRuntimeException(new ModuloMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}

	/* (non-Javadoc)
	 * @see co.gov.medellin.sdep.sisdep.regulacion.service.IModuloService#getModuloListByCriteria(co.gov.medellin.sdep.sisdep.model.Modulo)
	 */
	public List getModuloListByCriteria(Modulo moduloSearch) {
		try{
			return getModuloDAO().getModuloListByCriteria(moduloSearch);
		} catch (Exception e) {
			Object [] infoError = null;
			
			throw new ModuloRuntimeException(new ModuloMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	/* (non-Javadoc)
	 * @see co.gov.medellin.sdep.sisdep.regulacion.service.IModuloService#getModuloDisenoList()
	 */
	public GenericComponentVO getModuloDisenoList() {
		try{
			return getModuloDAO().getModuloDisenoList();
		} catch (Exception e) {
			Object [] infoError = null;
			
			throw new ModuloRuntimeException(new ModuloMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}
}
